package tree.binarySearchTree;

public class BinarySearch {
    public TreeNode root = null;
    public static void main(String[] args) {
        BinarySearch binarySearch = new BinarySearch();
        binarySearch.insert(10);
        binarySearch.insert(5);
        binarySearch.insert(2);
        binarySearch.insert(20);
        binarySearch.insert(16);
        binarySearch.inorder(binarySearch.root);
        TreeNode node = binarySearch.find(20);
        System.out.println("\n" + node.val);
        binarySearch.delete(3);
        binarySearch.inorder(binarySearch.root);
    }
    public TreeNode find(int val){  // 寻找值为val的节点
        TreeNode cur = root;
        while (cur != null){
            if(cur.val > val){
                cur = cur.left;
            } else if(cur.val == val){
                return cur;
            } else {
                cur = cur.right;
            }
        }
        return null;
    }
    public void insert(int val){  // 插入值为val的节点
        TreeNode node = new TreeNode(val);
        if(root == null){
            root = node;
            return;
        }
        TreeNode cur = root;
        TreeNode par = null;
        while (cur != null){
            if (cur.val > val) {
                par = cur;
                cur = cur.left;
            } else if(cur.val == val){
                return;
            } else {
                par = cur;
                cur = cur.right;
            }
        }
        if(val < par.val){
            par.left = node;
        } else {
            par.right = node;
        }
    }
    public void inorder(TreeNode root){  // 二叉搜索树的中序遍历应为从小到大
        if(root == null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    public void delete(int val){  // 删除值为val的节点
        TreeNode cur = root;
        TreeNode par = null;
        if(cur.left == null && cur.right == null){
            return;
        }
        while (cur != null){
            if(cur.val > val){
                par = cur;
                cur = cur.left;
            } else if(cur.val == val){
                removeTreeNode(par,cur);
                return;
            } else {
                par = cur;
                cur = cur.right;
            }
        }

    }

    private void removeTreeNode(TreeNode par, TreeNode cur) {
        if(cur.left == null) {                          // 删除父亲节点为par的cur节点
            if (cur == root) {
                root = cur.right;
            } else if(cur == par.left){
                par.left = cur.right;
            } else {
                par.right = cur.right;
            }
        } else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            } else if(cur == par.left) {
                par.left = cur.left;
            } else {
                par.right = cur.left;
            }
        } else {
            TreeNode target = cur.right;
            TreeNode targetPar = cur;
            while (target.left != null){
                targetPar = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetPar.left) {
                targetPar.left = target.right;
            } else {
                targetPar.right = target.right;
            }
        }
    }
}
